package com.sandu.mirrorapp.activity

import android.content.ClipData
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.blankj.utilcode.util.ToastUtils
import com.sandu.lib_common.base.BaseDBActivity
import com.sandu.lib_common.ext.showMessage
import com.sandu.lib_ui.utils.SettingUtil
import com.sandu.lib_common.utils.StatusBarUtil
import com.sandu.lib_mvvm.base.viewmodel.BaseViewModel
import com.sandu.lib_mvvm.ext.util.clipboardManager
import com.sandu.lib_mvvm.ext.view.clickNoRepeat
import com.sandu.mirrorapp.databinding.ActivityErrorBinding


/**
 * 作者　: sandu
 * 时间　: 2024/11/21
 * 描述　: 默认的错误页
 */
class ErrorActivity : BaseDBActivity<BaseViewModel, ActivityErrorBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolbar.toolbar.title = "发生错误"
        supportActionBar?.setBackgroundDrawable(ColorDrawable(SettingUtil.getColor(this)))
        StatusBarUtil.setColor(this, SettingUtil.getColor(this), 0)
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)
        mDatabind.errorRestart.clickNoRepeat {
            config?.run {
                CustomActivityOnCrash.restartApplication(this@ErrorActivity, this)
            }
        }
        mDatabind.errorSendError.clickNoRepeat {
            CustomActivityOnCrash.getStackTraceFromIntent(intent)?.let {
                showMessage(it, "发现有Bug不去打作者脸？", "必须打", {
                    val mClipData = ClipData.newPlainText("errorLog", it)
                    // 将ClipData内容放到系统剪贴板里。
                    clipboardManager?.setPrimaryClip(mClipData)
                    ToastUtils.showShort("已复制错误日志")
                    try {
                        val url = "mqqwpa://im/chat?chat_type=wpa&uin=824868922"
                        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
                    } catch (e: Exception) {
                        ToastUtils.showShort("请先安装QQ")
                    }
                }, "我不敢")
            }


        }
    }
}